<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Clipper1 vs Clipper2 Benchmark</title>
	<script src="./testPolyOperation.js"></script>
	<script src="./circlePath.js"></script>

</head>
<body>
	<script type="module">
		import * as clipperLib from "https://cdn.jsdelivr.net/npm/js-angusj-clipper@1.3.1/web/+esm";
		import Clipper2ZFactory from "../../dist/es/clipper2z.js";

		const promises = [
			clipperLib.loadNativeClipperLibInstanceAsync(clipperLib.NativeClipperLibRequestedFormat.WasmWithAsmJsFallback),
			Clipper2ZFactory({locateFile:()=>'../../dist/es/clipper2z.wasm'})
		];

		Promise.all(promises).then(([clipper, Clipper2Z]) => {
			const libs = [
				{ name: "clipper1",
				  clipper,
				},
				{ name: "clipper2",
				  clipper: Clipper2Z,
				}
			]

			for (const benchmark of [
				{ ops: 5000, points: 5000 },
				{ ops: 10000, points: 100 },
			]) {

				const poly1 = circlePath({ x: 1000, y: 1000 }, 1000, benchmark.points);
				const poly2 = circlePath({ x: 2500, y: 1000 }, 1000, benchmark.points);

				// convert to array of ints
				const poly1ArrayInt = poly1.map((p) => [p.x, p.y]).flat();
				const poly2ArrayInt = poly2.map((p) => [p.x, p.y]).flat();

				const poly1Paths = new Clipper2Z.Paths64();
				const poly2Paths = new Clipper2Z.Paths64();
				poly1Paths.push_back(Clipper2Z.MakePath64(poly1ArrayInt));
				poly2Paths.push_back(Clipper2Z.MakePath64(poly2ArrayInt));

				initPolyOperationClipper2(Clipper2Z, poly1Paths, poly2Paths);

				for (const clipType of [
					"Intersection",
					"Union",
					"Difference",
					"Xor",
				]) {
					for (const polyFillType of [
					"EvenOdd",
					// "NonZero",
					// "Negative",
					// "Positive",
					]) {
						for (const lib of libs) {
							const start = performance.now();

							for (let i = 0; i < benchmark.ops; i++) {
								if(lib.name === "clipper1"){
									testPolyOperationClipper1(lib.clipper, clipperLib.ClipType[clipType], clipperLib.PolyFillType[polyFillType], poly1, poly2);
								} else {
									testPolyOperationClipper2(lib.clipper, Clipper2Z.ClipType[clipType], Clipper2Z.FillRule[polyFillType], poly1Paths, poly2Paths);
								}
							}
							const end = performance.now();

							console.log(`${lib.name} ${clipType} ${polyFillType} ${benchmark.ops} ops ${benchmark.points} points: ${end - start} ms`);
						}
					}
				}
			};
		});
	</script>
</body>
</html>
